#include "includes.h"

#ifdef USE_TK

union16_t TkDat;							// Used to store TK data
union16_t TkWkDat;						// Used to store TK wakeup threshold data
union16_t TkWkTh;							// Touchkey wakeup threshold
bit TkWkUpFlag;								// TK wakeup flag


void TkInit(uint16_t ch)
{
	TKCSCF = 0x58;							//
	TKCSOF = 0x07;							// 
	TKCON0 = 0x17;							// Wait cpu read | ADC_FREQ = 1/8 SYS_CLK | Polling mode is open 
	TKCON1 = 0x30;							// 8 times convert | Data is valid and trigger interrupt
	TKCON2 = 0x03;							// Session gap = 16*clk_32
	TKADCF = 0x00;							// Vref=VDD 
	TKWKH0 = 0x00;							// Weakup threshold 0
	TKWKL0 = 0x00;							// Weakup threshold 0
	TKWKH1 = 0x00;							// Weakup threshold 1
	TKWKL1 = 0x01;							// Weakup threshold 1
	TKCHS1 = ch>>8;							// Touch key 15-8 select bit
	ASW1 = ch>>8;								// Analog switch
	TKCHS0 = ch&0xFF;						// Touch key 7-0 select bit
	ASW0 = ch&0xFF;							// Analog switch
	TKCON0 |= BIT7;							// Enable TK
	TKCON2 &= ~BIT2;						// Clear wait cpu read flag
	IRCON &= ~BIT5;							// Clear tdc flag
}

void TkNorDat(void)
{
	uint8_t ie0, ie1, tkcon1;
	
	ie0 = IE0;
	ie1 = IE1;
	tkcon1 = TKCON1;
	IE0 = 0;
	IE1 = 0;

	TKCON1 &= ~BIT2;						// Data is valid and trigger interrup
	TKCON1 |= BIT7;							// Trigger the analog function start working
	while(TKCON1&BIT7);					// Waitting for Tk scan finished
	TkDat.u8[0] = TKDATH;
	TkDat.u8[1] = TKDATL;
	IRCON &= ~BIT5;							// Clear tdc flag
	TKCON2 &= ~BIT2;						// Clear wait cpu read flag

	TKCON1 = tkcon1;
	IE1 = ie1;
	IE0 = ie0;
	
	SleepDlyFlag = 1;
}

void TkSlpDat(void)
{
	uint8_t ie0, ie1, tkcon1;

	ie0 = IE0;
	ie1 = IE1;
	tkcon1 = TKCON1;
	IE0 = 0;
	IE1 = 0;
	
	TKWKH1 = 0x00;							// Set TKWK1 to lowest value
	TKWKL1 = 0x00;
	IE1 |= BIT5;								// Enable TK interrupt
	EA = 1;
	TKCON1 |= BIT2;							// Key touch action trigger interrupt
	Sleep();
	TkWkUpFlag = 0;
	
	TKCON1 = tkcon1;
	IE0 = ie0;
	IE1 = ie1;
}



#ifdef USE_TK_DEMO
void TkDemoInit(void)
{
	PSFT1 |= BIT2;							// Shift TK12*-TK15*
	TkInit(0x2000);							// Open TK13*
	TkWkInit();
}

void TkWkInit(void)						// Scan the TK wakeup threshold
{
	TKCHS1 = 0x20;
	TKCHS0 = 0x00;
	TkSlpDat();
	TkWkTh.u16 = TkDat.u16+TKWKTH_BIS;
}

void TkNorDemoProc(void)			// Normal mode scan
{
	TKCHS1 = 0x20;
	TKCHS0 = 0x00;
	TkScanPreSet();
	TkNorDat();
	UartTxDat(TkDat.u8[0]);
	UartTxDat(TkDat.u8[1]);
	DelayMs(500);
}

void TkSlpDemoProc(void)			// Sleep mode wakeup
{
	TKCHS1 = 0x20;							// Set the channels to wakeup
	TKCHS0 = 0x00;
	TkWkPreSet();
	Sleep();
	if(TkWkUpFlag)							// Wakeup by TK
	{
		TkWkUpFlag = 0;
		if(TkDat.u16>TkWkTh.u16)
		{
			TkScanPreSet();
			TKCHS1 = 0x20;					// Set the channels to scan
			TKCHS0 = 0x00;
			TkNorDat();
			UartInit(BRT_8M_9600);
			UartTxDat(TkDat.u8[0]);
			UartTxDat(TkDat.u8[1]);
			UartDis();
		}
	}
}

void TkWkPreSet(void)					// TK sleep mode scan preset
{
	TKWKH1 = 0x00;							// Set TK wakeup threshold to the lowest value
	TKWKL1 = 0x00;
	TKCON1 = 0x14;							// 2 times convert time | Key touch action trigger interrupt
	TKCON2 = 0x53;							// Session gap = 2048*clk_32
	IE1 |= BIT5;								// Touch key interrupt enable
	EA = 1;
}

void TkScanPreSet(void)				// TK normal mode scan preset
{
	TKCON1 = 0x20;							// 2 times convert time | Data is valid and trigger interrupt
	TKCON2 = 0x03;							// Session gap = 16*clk_32
}



#endif	//USE_TK_DEMO



#endif	//USE_TK

